package cn.wolfcode.car.service.impl;

import cn.wolfcode.car.domain.Department;
import cn.wolfcode.car.domain.Employee;
import cn.wolfcode.car.domain.Permission;
import cn.wolfcode.car.domain.Role;
import cn.wolfcode.car.mapper.EmployeeMapper;
import cn.wolfcode.car.qo.EmployeeQueryObject;
import cn.wolfcode.car.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public int delete(Long id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int save(Employee record) {
        Md5Hash md5Hash = new Md5Hash(record.getPassword(), record.getName());
        record.setPassword(md5Hash.toString());
        return employeeMapper.insert(record);
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Employee> list(EmployeeQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> list = employeeMapper.selectForPage(qo);
        return new PageInfo<>(list);
    }

    @Override
    public int update(Employee record) {
        return employeeMapper.updateByPrimaryKey(record);
    }

    @Override
    public Employee getByUserName(String username) {
        return employeeMapper.getByUserName(username);

    }

    @Override
    public Workbook exportXls() {
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("员工信息");
        //表头
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("ID");
        row.createCell(1).setCellValue("用户名");
        row.createCell(2).setCellValue("姓名");
        row.createCell(3).setCellValue("密码");
        row.createCell(4).setCellValue("email");
        row.createCell(5).setCellValue("年龄");
        row.createCell(6).setCellValue("是否是管理员");
        row.createCell(7).setCellValue("部门ID");
        List<Employee> employees = employeeMapper.selectAll();
        for (int i = 0; i < employees.size(); i++) {
            Employee employee = employees.get(i);
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(employee.getId());
            row.createCell(1).setCellValue(employee.getUsername());
            row.createCell(2).setCellValue(employee.getName());
            row.createCell(3).setCellValue(employee.getPassword());
            row.createCell(4).setCellValue(employee.getEmail());
            row.createCell(5).setCellValue(employee.getAge());
//            row.createCell(6).setCellValue(employee.isAdmin());
            if (employee.getDept() != null) {
                row.createCell(7).setCellValue(employee.getDept().getId());
            } else {
                row.createCell(7).setCellValue("");
            }
        }
        return wb;
    }

    @Override
    public void deleteAll(Long[] ids) {
        employeeMapper.deleteAll(ids);
    }

    @Override
    public List<Role> getRolesAndPermission(Long id) {
        return employeeMapper.getRolesAndPermission(id);
    }

    @Override
    public List<Permission> getPermission(Long id) {
        return employeeMapper.getPermission(id);
    }

    @Override
    public List<Employee> listAll() {

        return employeeMapper.listAll();
    }
}
